/**
 * 
 */
package cloud.simple.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import cloud.simple.model.User;

/**
 * @author zhaodarong
 *
 */
@Service
public class UserService {
	public static final String SERVICE_NAME = "cloud-simple-service";
	
	
	@Autowired
	private RestTemplate restTemplate = new RestTemplate();
	
	@SuppressWarnings("unchecked")
	@HystrixCommand(fallbackMethod = "fallbackSearchAll")
	public List<User> searchAll() {
		return restTemplate.getForObject("http://" + SERVICE_NAME + "/user", List.class);
/*		String result = null;
		result = restTemplate.getForObject("http://" + SERVICE_NAME + "/user", String.class);
		System.out.println("result=[" + result + "]");
		
		return null;
*/	}
	
	private List<User> fallbackSearchAll() {
		System.out.println("HystrixCommand fallbackMethod handle");
		List<User> ls = new ArrayList<>();
		User user = new User();
		user.setUsername("HystrixTest");
		ls.add(user);
		
		return ls;
	}
	
	@LoadBalanced
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
}
